<template>
    <el-select v-model="values"
               :placeholder="placeholder"
               :loading="loading"
               :remote-method="searchRemote"
               @change="inner_change"
               multiple
               filterable
               remote
               allow-create
               default-first-option
               reserve-keyword>
        <el-option v-for="(c,index) in categories"
                   :value="c"
                   :key="index"
                   :label="c">
        </el-option>
    </el-select>
</template>
<script>
    let unwatch;
    export default {
        name: "anchor_search_category",
        props: {
            placeholder: {type: String, default: '分类名称'},
            value: {required: true, type: Array},
        },
        data() {
            return {
                loading: false,
                values: [],
                categories: [],
            };
        },
        methods: {
            searchRemote(search) {
                this.loading = true;
                this.$http.get('/api/proposal/categories', {
                    params: {search}
                }).then(res => {
                    this.categories = res.data;
                    this.loading = false;
                });
            },
            inner_change() {
                unwatch();
                this.$emit('input', this.values);
                this.$emit('change', this.values);
            },
        },
        mounted() {
            unwatch = this.$watch('value', function (val) {
                this.values.push(...val);
                unwatch();
            });
        },
    }
</script>

<style scoped>
</style>